#include <wiisprite.h>
#include <fat.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <wiiuse/wpad.h>
#include <asndlib.h>
#include <fcntl.h>
#include "oggplayer.h"
#include "missilecommand.h"
#include "gameaction.h"
#include "gametrigger.h"

using namespace wsp;

extern u32 MAXMISSILES;

Game *game;

int main(int argc, char **argv)
{
  srand(time(NULL));

  GameWindow gwd;
  gwd.InitVideo();
  WPAD_Init();
  WPAD_SetDataFormat(WPAD_CHAN_0, WPAD_FMT_BTNS_ACC_IR);

  fatInitDefault();

  SND_Init(INIT_RATE_48000);
  SND_Pause(0);
  
// Sorry, broken because I'm too dumb to make it work. For now.
//  PlayOgg((const void*)open(MEDIAPATH"bgm.ogg", O_RDONLY, 0), 1, 0, OGG_INFINITE_TIME);

  gwd.SetBackground((GXColor){0,0,255,255});

  game = new Game();
  game->initialize();
  game->activate();

  GameActionList maingame;
  GameActionList gameover;

  /**
   * Title screen
   **/
  maingame.add(new ReInitializeAction());
  maingame.add(new ShowTextAction("Press B or Z to activate missile defense system.", 0, 0));
  maingame.add(new PauseAction());
  maingame.add(new DisableGameInputAction());
  maingame.add(new SetFadeAction(.2));
  maingame.add(new WaitButtonPressAction(WPAD_BUTTON_B | WPAD_NUNCHUK_BUTTON_Z));

  /**
   * Start game
   **/
  maingame.add(new HideTextAction());
  maingame.add(new ShowTextAction("Incoming...", 0, 0));
  maingame.add(new DelayAction(1));
  maingame.add(new ResumeAction());
  maingame.add(new EnableGameInputAction());
  maingame.add(new RegisterTriggerAction(new NoSilosTrigger(new LoadActionListAction(&gameover))));
  maingame.add(new FadeToAction(1));
  maingame.add(new DelayAction(180));
  maingame.add(new FadeOutTextAction());
  
  /**
   * Game over
   **/
  gameover.add(new ShowTextAction("It's over. Press B or Z to try again.", 0, 0));
  gameover.add(new FadeToAction(.2));
  gameover.add(new WaitButtonPressAction(WPAD_BUTTON_B | WPAD_NUNCHUK_BUTTON_Z));
  gameover.add(new ReInitializeAction());
  gameover.add(new LoadActionListAction(&maingame));

  game->al = &maingame;

  game->al->begin();

  while(1) {
	WPAD_ScanPads();

	/**
	 * Track where we're pointing
	 **/
    struct ir_t ir;
    WPAD_IR(0,&ir);
    game->px = ir.sx;
    game->py = ir.sy;
    game->enableCrosshairs = ir.num_dots > 0;
	
	/**
	 * Button handling
	 **/

	/* Exit if Home held */
	if(WPAD_ButtonsDown(0)&WPAD_BUTTON_HOME)
			break;
	
	/* A little Easter egg if you press 2 */
    if(WPAD_ButtonsDown(0)&WPAD_BUTTON_PLUS) {
      if(WPAD_ButtonsHeld(0) & WPAD_BUTTON_1)
        MAXMISSILES = 10;
      else if(WPAD_ButtonsHeld(0) & WPAD_BUTTON_2)
        MAXMISSILES = 300;
      game->initialize();
      game->activate();
      game->al = &maingame;
      game->al->begin();
    }
    if(WPAD_ButtonsDown(0)&WPAD_BUTTON_MINUS) {
      game->setFade(0.0);
    } else if(WPAD_ButtonsUp(0)&WPAD_BUTTON_MINUS) {
      game->setFade(1.0);
    }

    game->al->perform();
    vector<GameTrigger*>::iterator tit;
    vector<GameTrigger*> ttoremove;
    for(tit = game->triggers.begin(); tit != game->triggers.end(); ++tit) {
      GameTrigger *trigger = *tit;
      if(trigger->test()) {
        game->triggers.erase(tit);
        break;
      }
    }

	game->update();
	game->draw();

	gwd.Flush();
	}
}
